package com.china08.yxyapi.api.manage.tec.teaching.homework.uploadques;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.entity.db.yxy.YxyQuesKnow;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.repository.db.YxyQuesKnowRepository;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class homeworkKnowSchController {

    @Autowired
    private YxyQuesKnowRepository yxyQuesKnowRepository;

    @ApiOperation(value = "作业系统上传试题， 查询某个知识知识")
    @RequestMapping(value = "/api_yw/manage/tec/homework/question/know", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody ReqModel reqModel) {
        List<RespModel> respModelList = new ArrayList<homeworkKnowSchController.RespModel>();
        List<YxyQuesKnow> yxyQuesKnowList = yxyQuesKnowRepository
                .findByStageAndSubjectAndDeleteFlagAndParentId(reqModel.phase, reqModel.subject, "1", reqModel.knowId);
        for (YxyQuesKnow yxyQuesKnow : yxyQuesKnowList) {
            RespModel model = new RespModel();
            model.id = yxyQuesKnow.getId();
            model.name = yxyQuesKnow.getName();
            respModelList.add(model);
        }
        return respModelList;
    }

    protected static class ReqModel extends YxyApiReqModel {
        public String phase;
        public String subject;
        public Integer knowId;

        @Override
        public void autoCheck() {
            Assert.notNull(StringUtils.defaultIfBlank(phase, null), "找不到学段请刷新页面。");
            Assert.notNull(StringUtils.defaultIfBlank(subject, null), "找不到学段请刷新页面。");
            Assert.notNull(knowId, "找不到知识点请刷新页面。");
        }
    }

    protected static class RespModel {
        public Integer id;
        public String name;
    }
}
